import pymysql
class MysqlUtil:
    def __init__(self,host,port,username,password,db,charset):
        self.conn = pymysql.connect(host=host,port=port,username=username,password=password,database=db,charset=charset)
        # 设置自动提交
        self.conn.autocommit(True)
        self.cursor = self.conn.cursor()

    def query(self,sql):
        self.cursor.execute(sql)
        return self.cursor.fetchall()

    def update(self,sql,params=None):

        try:
            self.cursor.execute(sql, params)
            self.conn.commit()
        except Exception as result:
            print(result)
            #回滚
            self.conn.rollback()


    def close(self):
        self.conn.close()
        self.cursor.close()